package com.app.core.util;

import android.content.Context;
import android.graphics.Region;


import com.specialtreasure.freightdepot.model.SCity;
import com.specialtreasure.freightdepot.model.SProvince;
import com.specialtreasure.freightdepot.model.SRegion;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

/**
 * Created by KUMA on 2017/1/11.
 */
public class XmlParser {
    public static List<SProvince> parseCity(Context context) throws IOException, ParserConfigurationException, SAXException {
        InputStream is = context.getAssets().open("china.xml");
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.parse(is);
        Element root = document.getDocumentElement();
        NodeList provinceNodes = root.getChildNodes();

        List<SProvince> provinces = new ArrayList<>();
        if (provinceNodes != null && provinceNodes.getLength() > 0) {
            for (int i = 0; i < provinceNodes.getLength(); i++) {
                Node node = provinceNodes.item(i);
                if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equals("a")) {
                    Element provinceElement = (Element) provinceNodes.item(i);
                    SProvince province = new SProvince();
                    province.name = provinceElement.getAttribute("name");
                    NodeList cityNodes = provinceElement.getChildNodes();
                    if (cityNodes != null && cityNodes.getLength() > 0) {
                        for (int j = 0; j < cityNodes.getLength(); j++) {
                            Node cityNode = cityNodes.item(j);
                            if (cityNode.getNodeType() == Node.ELEMENT_NODE && cityNode.getNodeName().equals("a")) {
                                Element cityElement = (Element) cityNodes.item(j);
                                SCity city = new SCity();
                                city.name = cityElement.getAttribute("name");
                                NodeList regionNodes = cityElement.getChildNodes();
                                if (regionNodes != null && regionNodes.getLength() > 0) {
                                    for (int k = 0; k < regionNodes.getLength(); k++) {
                                        Node regionNode = regionNodes.item(k);
                                        if (regionNode.getNodeType() == Node.ELEMENT_NODE && regionNode.getNodeName().equals("a")) {
                                            Element regionElement = (Element) regionNodes.item(k);
                                            SRegion region = new SRegion();
                                            region.name = regionElement.getAttribute("name");
                                            city.regions.add(region);
                                        }
                                    }
                                }
                                province.cities.add(city);
                            }
                        }
                    }
                    provinces.add(province);
                }
            }
        }
        return provinces;
    }

    public static List<String> parseGoods(Context context) throws IOException, ParserConfigurationException, SAXException {
        InputStream is = context.getAssets().open("huowu.xml");
        return parse(is);
    }

    public static List<String> parseTruckType(Context context) throws IOException, ParserConfigurationException, SAXException {
        InputStream is = context.getAssets().open("chexing.xml");
        return parse(is);
    }

    public static List<String> parseTruckLength(Context context) throws IOException, ParserConfigurationException, SAXException {
        InputStream is = context.getAssets().open("chechang.xml");
        return parse(is);
    }

    public static List<String> parseUnit(Context context) throws IOException, ParserConfigurationException, SAXException {
        InputStream is = context.getAssets().open("danwei.xml");
        return parse(is);
    }

    public static List<String> parseXml(Context context, String xmlName) throws IOException, ParserConfigurationException, SAXException {
        InputStream is = context.getAssets().open(xmlName);
        return parse(is);
    }


    public static List<String> parse(InputStream is) throws ParserConfigurationException, IOException, SAXException {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.parse(is);
        Element root = document.getDocumentElement();
        NodeList nodes = root.getChildNodes();
        List<String> list = new ArrayList<>();
        if (nodes != null && nodes.getLength() > 0) {
            for (int i = 0; i < nodes.getLength(); i++) {
                Node node = nodes.item(i);
                if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equals("a")) {
                    Element element = (Element) nodes.item(i);
                    list.add(element.getAttribute("name"));
                }
            }
        }
        return list;
    }
}
